﻿#include "GxImageSetting.h"
#include "ui_GxImageSetting.h"
#include <QAbstractItemModel>
#include <QFileDialog>
#include <QModelIndex>
#include <QEventLoop>
#include <QShortcut>
#include <QKeySequence>
#include <QApplication>
#include <QDebug>
#include "util.h"

GxImageSetting::GxImageSetting(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::GxImageSetting)
    , m_pEventLoop(nullptr)
{
    ui->setupUi(this);

    QRect rect = util::getSettingSetGeometry();
    if (rect.x() >= 0 && rect.y() >= 0)
    {
        setGeometry(rect);
    }

    setWindowTitle(tr("Image Gadget Setting"));

    setWindowFlags(Qt::Dialog);
    setAttribute(Qt::WA_TranslucentBackground, false);

    QShortcut* pExit = new QShortcut(QKeySequence(Qt::Key_Escape), this, SLOT(close()), nullptr, Qt::WindowShortcut);
    pExit->setAutoRepeat(false);

    this->installEventFilter(this);

    //获取当前切换间隔
    ui->sbInterval->setValue(util::getSettingInterval());

    connect(ui->btnAdd, &QPushButton::clicked, [=]()
    {
        QString strDir = QFileDialog::getExistingDirectory(this, tr("Select Image Dir"), util::getSettingLastDir());
        if (strDir.isEmpty())
            return;
        emit signalToAddDir(strDir); 
        util::saveSettingLastDir(strDir);
    });

    connect(ui->btnRemove, &QPushButton::clicked, [=]()
    {
        QString strDir = ui->listView->currentIndex().data(Qt::EditRole).toString();
        if(strDir.isEmpty())
        {
            return;
        }
        emit signalToRemoveDir(strDir); 
    });

    connect(ui->btnResetGeometry, &QPushButton::clicked, this, &GxImageSetting::signalResetGeometry);


    connect(ui->sbInterval, QOverload<int>::of(&QSpinBox::valueChanged), this, &GxImageSetting::signalIntervalChanged);

    //构造语言列表
    QStringList listLanguages = util::getLanguages();
    ui->cbLanguage->clear();
    foreach(QString str, listLanguages)
    {
        //翻译变量
        ui->cbLanguage->addItem(tr("%1", qPrintable(str)).arg(str), str);
    }
    ui->cbLanguage->setCurrentIndex(ui->cbLanguage->findData(util::getCurrentLanguage()));
    connect(ui->cbLanguage, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &GxImageSetting::slotLanguageIndexChanged);
}

GxImageSetting::~GxImageSetting()
{
    delete ui;
}

void GxImageSetting::show()
{
    QWidget::show();
    QEventLoop loop;
    m_pEventLoop = &loop;
    loop.exec();
}

void GxImageSetting::setModel(QAbstractItemModel* model)
{
    ui->listView->setModel(model);
}

bool GxImageSetting::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == this)
    {
        switch (event->type())
        {
            //移动缩放事件
            case QEvent::Resize:
            case QEvent::Move:
            {
                util::saveSettingSetGeometry(geometry());
            }
            break;

            case QEvent::Close:
            {
                if(m_pEventLoop)
                {
                    m_pEventLoop->exit(0);
                    m_pEventLoop = nullptr;
                }
            }

            case QEvent::LanguageChange:
            {
                //记录combobox之前的index
                int iLanguageIndex = ui->cbLanguage->currentIndex();
                ui->cbLanguage->blockSignals(true);
                
                //重新设置字体以翻译
                ui->retranslateUi(this);

                //设置combobox回之前的序号
                ui->cbLanguage->setCurrentIndex(iLanguageIndex);
                ui->cbLanguage->blockSignals(false);

                //标题
                setWindowTitle(tr("Image Gadget Setting"));
            }
            break;

            default:
                break;
        }

    }

    return QWidget::eventFilter(watched, event);
}

void GxImageSetting::slotLanguageIndexChanged(int index)
{
    QString strLanguage = ui->cbLanguage->currentData().toString();
    util::setLanguage(strLanguage);
}
